package smartlifev30.com.sdk.api.service_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.util.Tools;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.bean.request.GatewayAddUserRequest;
import smartlifev30.com.sdk.bean.request.GatewayAdminApplyRequest;
import smartlifev30.com.sdk.bean.request.GatewayDelUserRequest;
import smartlifev30.com.sdk.bean.request.GatewayEditUserRequest;
import smartlifev30.com.sdk.bean.request.GatewaySet2Admin;
import smartlifev30.com.sdk.bean.request.GatewayUserJoinRequest;
import smartlifev30.com.sdk.bean.request.GatewayUserQuit;
import smartlifev30.com.sdk.bean.request.ListGatewayUserRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;

/**
 * Created by tys on 2018/8/12.
 */

public class GatewayUserImpl implements IGatewayUser {

    @Override
    public void listGatewayUser(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull CallBack<LoginResponse> callback) {
        ListGatewayUserRequest listGatewayUserRequest = new ListGatewayUserRequest();
        listGatewayUserRequest.setFrom(userName);
        listGatewayUserRequest.setMsg_id(callback.getMsgId());
        listGatewayUserRequest.setMsg_class("gwuser_mgmt");
        listGatewayUserRequest.setMsg_name("gw_userlist");
        listGatewayUserRequest.setMsg_type("get");
        listGatewayUserRequest.setGateway(listGatewayUserRequest.new Gateway(sn));
        listGatewayUserRequest.setToken(token);
        NetManager.getInstance().sendMessage(JsonUtil.toString(listGatewayUserRequest));
    }

    @Override
    public void gatewayAddUser(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum, @NonNull String userAlias, @Nullable Long startTime, @Nullable Long endTime, @NonNull CallBack<LoginResponse> callback) {
        GatewayAddUserRequest gatewayAddUserRequest = new GatewayAddUserRequest();
        gatewayAddUserRequest.setFrom(userName);
        gatewayAddUserRequest.setMsg_id(callback.getMsgId());
        gatewayAddUserRequest.setMsg_class("gwuser_mgmt");
        gatewayAddUserRequest.setMsg_name("gw_user_add");
        gatewayAddUserRequest.setMsg_type("get");
        GatewayAddUserRequest.User user = gatewayAddUserRequest.new User();
        user.setSn(sn);
        user.setPhone_num(phoneNum);
        user.setUser_alias(userAlias);
        if (startTime != null) {
            user.setValid_time_start(Tools.formatTime(startTime, null));
        }
        if (endTime != null) {
            user.setValid_time(Tools.formatTime(endTime, null));
        }
        gatewayAddUserRequest.setUser(user);
        gatewayAddUserRequest.setToken(token);
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayAddUserRequest));
    }

    @Override
    public void gatewayDelUser(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum,
                               @NonNull CallBack<LoginResponse> callback) {
        GatewayDelUserRequest gatewayDelUserRequest = new GatewayDelUserRequest();
        gatewayDelUserRequest.setFrom(userName);
        gatewayDelUserRequest.setMsg_id(callback.getMsgId());
        gatewayDelUserRequest.setMsg_class("gwuser_mgmt");
        gatewayDelUserRequest.setMsg_name("gw_user_del");
        gatewayDelUserRequest.setMsg_type("get");
        gatewayDelUserRequest.setToken(token);
        gatewayDelUserRequest.setUser(gatewayDelUserRequest.new User(sn, phoneNum));
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayDelUserRequest));
    }

    @Override
    public void gatewayEditUser(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum, @NonNull String userAlias, @Nullable Long startTime, @Nullable Long endTime, @NonNull CallBack<LoginResponse> callback) {
        GatewayEditUserRequest gatewayAddUserRequest = new GatewayEditUserRequest();
        gatewayAddUserRequest.setFrom(userName);
        gatewayAddUserRequest.setMsg_id(callback.getMsgId());
        gatewayAddUserRequest.setMsg_class("gwuser_mgmt");
        gatewayAddUserRequest.setMsg_name("gw_user_edit");
        gatewayAddUserRequest.setMsg_type("get");
        GatewayEditUserRequest.User user = gatewayAddUserRequest.new User();
        user.setSn(sn);
        user.setPhone_num(phoneNum);
        user.setUser_alias(userAlias);
        if (startTime != null) {
            user.setValid_time_start(Tools.formatTime(startTime, null));
        }
        if (endTime != null) {
            user.setValid_time(Tools.formatTime(endTime, null));
        }
        gatewayAddUserRequest.setUser(user);
        gatewayAddUserRequest.setToken(token);
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayAddUserRequest));
    }

    @Override
    public void gatewaySet2Admin(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum, @NonNull CallBack<LoginResponse> callback) {
        GatewaySet2Admin gatewaySet2Admin = new GatewaySet2Admin();
        gatewaySet2Admin.setFrom(userName);
        gatewaySet2Admin.setMsg_id(callback.getMsgId());
        gatewaySet2Admin.setMsg_class("gwuser_mgmt");
        gatewaySet2Admin.setMsg_name("gw_set2admin");
        gatewaySet2Admin.setMsg_type("set");
        gatewaySet2Admin.setToken(token);
        gatewaySet2Admin.setUser(gatewaySet2Admin.new User(sn, phoneNum));
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewaySet2Admin));
    }

    @Override
    public void gatewayUserQuit(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull CallBack<LoginResponse> callback) {
        GatewayUserQuit gatewayUserQuit = new GatewayUserQuit();
        gatewayUserQuit.setFrom(userName);
        gatewayUserQuit.setMsg_id(callback.getMsgId());
        gatewayUserQuit.setMsg_class("gwuser_mgmt");
        gatewayUserQuit.setMsg_name("gw_user_quit");
        gatewayUserQuit.setMsg_type("set");
        gatewayUserQuit.setToken(token);
        gatewayUserQuit.setUser(gatewayUserQuit.new User(sn));
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayUserQuit));
    }

    @Override
    public void gatewayUserJoin(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String gatewayAlias, @NonNull CallBack<LoginResponse> callback) {
        GatewayUserJoinRequest gatewayUserJoinRequest = new GatewayUserJoinRequest();
        gatewayUserJoinRequest.setFrom(userName);
        gatewayUserJoinRequest.setMsg_id(callback.getMsgId());
        gatewayUserJoinRequest.setMsg_class("gwuser_mgmt");
        gatewayUserJoinRequest.setMsg_name("gw_apply_join");
        gatewayUserJoinRequest.setMsg_type("set");
        gatewayUserJoinRequest.setToken(token);
        gatewayUserJoinRequest.setParms(gatewayUserJoinRequest.new Parms(sn, gatewayAlias));
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayUserJoinRequest));
    }

    @Override
    public void gatewayAdminApply(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum, @NonNull String gatewayAlias, @NonNull Integer action, @NonNull CallBack<LoginResponse> callback) {
        GatewayAdminApplyRequest gatewayAdminApplyRequest = new GatewayAdminApplyRequest();
        gatewayAdminApplyRequest.setFrom(userName);
        gatewayAdminApplyRequest.setMsg_id(callback.getMsgId());
        gatewayAdminApplyRequest.setMsg_class("gwuser_mgmt");
        gatewayAdminApplyRequest.setMsg_name("gw_admin_apply");
        gatewayAdminApplyRequest.setMsg_type("set");
        gatewayAdminApplyRequest.setToken(token);
        GatewayAdminApplyRequest.Parms parms = gatewayAdminApplyRequest.new Parms();
        parms.setGatewaySn(sn);
        parms.setGatewayAlias(gatewayAlias);
        parms.setPhone_num(phoneNum);
        parms.setAction(action);
        gatewayAdminApplyRequest.setParms(parms);
        NetManager.getInstance().sendMessage(JsonUtil.toString(gatewayAdminApplyRequest));
    }

}
